package com.hengxinguotong.zhihuichengjian.ui.safetystudy;

import com.andexert.calendarlistview.library.DatePickerController;
import com.andexert.calendarlistview.library.DayPickerView;
import com.andexert.calendarlistview.library.SimpleMonthAdapter;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2018/10/31
 */

public class CalendarListActivity extends BaseActivity implements View.OnClickListener, DatePickerController {
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.today_tv)
    HXTextView todayYv;
    @BindView(R.id.toweek_tv)
    HXTextView toweekTv;
    @BindView(R.id.tomonth_tv)
    HXTextView tomonthTv;
    @BindView(R.id.toyear_tv)
    HXTextView toyearTv;
    @BindView(R.id.pickerView)
    DayPickerView pickerView;

    private Date firstDate, lastDate;
    private boolean hasAll = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar_list);
        ButterKnife.bind(this);
        initView();
    }

    private void initView(){
        titleTv.setText(R.string.date_select);
        pickerView.setController(this);
    }

    @OnClick({R.id.iv_back, R.id.today_tv, R.id.toweek_tv, R.id.tomonth_tv, R.id.toyear_tv})
    public void onClick(View v) {
        Calendar calendar = Calendar.getInstance();
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.today_tv:
                todayYv.setEnabled(false);
                lastDate = calendar.getTime();
                firstDate = calendar.getTime();
                exit();
                break;
            case R.id.toweek_tv:
                toweekTv.setEnabled(false);
                calendar.set(Calendar.DAY_OF_WEEK, 1);
                firstDate = calendar.getTime();
                calendar.set(Calendar.DAY_OF_WEEK, 7);
                lastDate = calendar.getTime();
                exit();
                break;
            case R.id.tomonth_tv:
                tomonthTv.setEnabled(false);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                firstDate = calendar.getTime();
                calendar.add(Calendar.MONTH, 1);
                calendar.add(Calendar.DAY_OF_MONTH, -1);
                lastDate = calendar.getTime();
                exit();
                break;
            case R.id.toyear_tv:
                toyearTv.setEnabled(false);
                calendar.set(Calendar.MONTH, 0);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                firstDate = calendar.getTime();
                calendar.add(Calendar.YEAR, 1);
                calendar.add(Calendar.DAY_OF_YEAR, -1);
                lastDate = calendar.getTime();
                exit();
                break;
        }
    }

    @Override
    public Calendar getMinYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2017);
        return calendar;
    }

    @Override
    public Calendar getMaxYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar;
    }

    @Override
    public void onDayOfMonthSelected(int year, int month, int day) {
        Log.e("Day Selected", day + " / " + month + " / " + year);
    }

    @Override
    public void onDateRangeSelected(SimpleMonthAdapter.SelectedDays<SimpleMonthAdapter.CalendarDay> selectedDays) {
        Date startDate = selectedDays.getFirst().getDate();
        Date endDate = selectedDays.getLast().getDate();
        sort(startDate, endDate);
    }

    private void sort(Date startDate, Date endDate){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        int startYear = calendar.get(Calendar.YEAR);
        int startMonth = calendar.get(Calendar.MONTH);
        int startDay = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.setTime(endDate);
        int endYear = calendar.get(Calendar.YEAR);
        int endMonth = calendar.get(Calendar.MONTH);
        int endDay = calendar.get(Calendar.DAY_OF_MONTH);
        if (startYear < endYear){
            firstDate = startDate;
            lastDate = endDate;
        }else if(startYear == endYear){
            if(startMonth < endMonth){
                firstDate = startDate;
                lastDate = endDate;
            }else if(startMonth == endMonth){
                if(startDay <= endDay){
                    firstDate = startDate;
                    lastDate = endDate;
                }else {
                    firstDate = endDate;
                    lastDate = startDate;
                }
            }else {
                firstDate = endDate;
                lastDate = startDate;
            }
        }else {
            firstDate = endDate;
            lastDate = startDate;
        }

        exit();
    }

    private void exit(){
        Intent intent = getIntent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("firstDate", firstDate);
        bundle.putSerializable("lastDate", lastDate);
        intent.putExtra("bundle", bundle);
        setResult(RESULT_OK, intent);
        finish();
    }

    private Date getTime(int year, int month, int week, int day){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, year);
        calendar.add(Calendar.MONTH, month);
        calendar.add(Calendar.WEEK_OF_MONTH, week);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }
}
